package com.guomei.service.menu.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.*;
import com.guomei.mapper.MenuMapper;
import com.guomei.service.menu.MenuService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单管理
 */
@Service
@Slf4j
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;



    @Override
    public Result<?> addMenu(AddMenuDTO addMenuDTO) {
        Menu menu = new Menu();
        menu.setParentId(addMenuDTO.getParentId());
        menu.setMenuName(addMenuDTO.getMenuName());
        menu.setMenuUrl(addMenuDTO.getMenuUrl());
        menu.setMenuIcon(addMenuDTO.getMenuIcon());
        menu.setSortOrder(addMenuDTO.getSortOrder());
        menu.setMenuType(addMenuDTO.getMenuType());
        menu.setPermissions(addMenuDTO.getPermissions());
        menuMapper.insert(menu);
        return Result.success("插入菜单信息成功");
    }

    @Override
    public Result<?> deleteMenu(DeleteMenuDTO deleteMenuDTO) {
        menuMapper.deleteById(deleteMenuDTO.getId());

        return Result.success("删除菜单信息成功");
    }
//
//    @Override
//    public Result<?> updateMenu(UpdateMenuDTO updateMenuDTO) {
//        return null;
//    }
//
    @Override
    public Result<?> getMenuList(GetMenuListDTO getMenuListDTO) {
        Menu menu = new Menu();
        menu.setParentId(getMenuListDTO.getParentId());
        menu.setMenuName(getMenuListDTO.getMenuName());
        // 1. 设置分页参数（在执行查询前设置）
        // 使用PageHelper进行分页查询
        PageHelper.startPage(getMenuListDTO.getPageNum(), getMenuListDTO.getPageSize());
        List<Menu> menus = menuMapper.selectAll(menu);
        // 封装分页结果到PageData
        PageData<Menu> pageData = new PageData<>();
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(getMenuListDTO.getPageNum());
        pageData.setPageSize(getMenuListDTO.getPageSize());
        return Result.success(pageData);
    }
}
